package com.mcb_wxc.aop;

import com.mcb_wxc.config.ProjectOperation;
import com.mcb_wxc.customAnnotation.ProjectRecord;
import com.mcb_wxc.util.IntUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

/**
 * Created by 小生
 * 2019/1/15 9:23.
 * 作用：项目的数据记录
 */
@Aspect // 切面标识
@Component // 交给spring容器管理
@Slf4j
public class ProjectLog {

    /**
     * 选取切入点为自定义注解哈
     */
    @Pointcut("@annotation(com.mcb_wxc.customAnnotation.ProjectRecord)")
    public void projectRecord(){}

    @Before(value = "projectRecord()")
    public void before(JoinPoint joinPoint) throws NoSuchMethodException {
    }
    @After(value = "projectRecord()")
    public void after(JoinPoint joinPoint) {
        // 获取连接点的方法签名对象
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        //获取目标类
        Object target = joinPoint.getTarget();
        // 获取到当前执行的方法
        Method method = null;
        try {
            method = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
        } catch (NoSuchMethodException e) {
            log.error("获取当前执行方法失败----" + e.getMessage());
            return;
        }
        //获取方法的注解
        ProjectRecord annotation = method.getAnnotation(ProjectRecord.class);
        //获取方法上的注解的值
        String operationModule = annotation.operationModule(); //操作模块
        //获取执行方法的所有参数值
        Object[] args = joinPoint.getArgs();
        String openId = null;
        Integer projectId = null;
        //获取执行方法的所有参数名
        for(int i=0;i<methodSignature.getParameterNames().length;i++){
            if("openId".equals(methodSignature.getParameterNames()[i])) { //根据参数名获取到openid参数的下标
                if(args[i] != null)
                    openId = args[i] + "";
            }
            if("projectId".equals(methodSignature.getParameterNames()[i])) { //根据参数名获取到openid参数的下标
                if(args[i] != null)
                    projectId = Integer.parseInt(args[i]+"");
            }
            if("openId".equals(methodSignature.getParameterNames()[i])) { //根据参数名获取到openid参数的下标
                if(args[i] != null)
                    openId = args[i] + "";
            }
            if("projectId".equals(methodSignature.getParameterNames()[i])) { //根据参数名获取到openid参数的下标
                if(args[i] != null)
                    projectId = Integer.parseInt(args[i]+"");
            }
        }
        if (openId != null && projectId != null && operationModule != null) {
            //项目数据记录（项目浏览量，分享数量，评价数量，收藏数量等）
            addProjectData(projectId,operationModule);
            //addUserVisit(openId,projectId);
        }
    }

    /**
     * 添加项目数据记录（项目浏览量，分享数量，评价数量，收藏数量等）
     * @param projectId
     * @param operationModule
     */
    public void addProjectData(Integer projectId, String operationModule) {

    }

    /**
     * 添加用户浏览项目记录
     * @param openId
     * @param
     */
    public void addUserVisit(String openId,Integer projectId) {

    }

    /**
     * 获取方法的返回数据 比after还要后面执行 暂时用不上
     * @param joinPoint
     * @param rvt 运行方法返回值
     * @return
     */
    @AfterReturning(returning="rvt", pointcut="@annotation(com.mcb_wxc.customAnnotation.ProjectRecord)")
    public void AfterExec(JoinPoint joinPoint,Object rvt){
    }
}
